 import {
   request
 } from '../../api/index'
 import recipeApi from '../../api/recipe'
 const app = getApp();

 Page({

   /**
    * 页面的初始数据
    */
   data: {
     id: '',
     sortNumber: 0,
     pageTitle: '',
     next: '',
     navigation: 0,
     glazeId: 0,
     dataSource: [],
     columnsList: [],
     columns: [],
     current: 0
   },
   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function (options) {
     this.setPageProperty(options);
     this.fetchRecipeList();
   },
   /**
    * 配方列表
    */
   fetchRecipeList(successCallback) {
     wx.showLoading({
       title: '加载中'
     })
     recipeApi.recipeList({
       status: 'ANNOUNCED'
     }, (e) => {

       this.setData({
         columnsList: e,

         columns: e.map(it => {
           return {
             text: it.name,
             value: it.id
           }
         })
       })
       if (successCallback instanceof Function) {
         successCallback();
       } else {
         this.fetchData();
       }
       wx.hideLoading()
     })
   },
   /**
    * 设置页面属性
    */
   setPageProperty({
     id,
     sortNumber = 0,
     pageTitle = '坯体釉料1',
     next = 'printing',
     navigation = 0
   }) {
     this.setData({
       id,
       pageTitle,
       sortNumber,
       next,
       fromNavigation: navigation,
       isRoot: app.globalData.root,
       recordState: app.globalData.editState
     })
     wx.setNavigationBarTitle({
       title: pageTitle
     })

   },
   fetchData() {
     request.glazeList({
       collectionRecordIds: this.data.id,
       sortNumber: this.data.sortNumber
     }, (res) => {

       this.setData({
         glazeId: res?.[0]?.id,
         dataSource: res?.[0]?.glazeDatum
       })
     })
   },
   onAdd() {

     const dataSource = this.data.dataSource || [];
     console.log(dataSource)
     if (dataSource.length > 2) {
       wx.showToast({
         title: '超出上限!',
         icon: 'none'
       })
       return
     }
     this.setData({
       dataSource: [...dataSource, {}]
     })

     this.setData({
       current: dataSource.length
     })

   },
   onRemove(event) {
     const idx = event.detail;
     this.setData({
       dataSource: this.data.dataSource.filter((_, i) => i !== idx),
       current: 0
     })
   },
   onChange(event) {
     const [values, idex] = event.detail;


     const dataSource = this.data.dataSource;
     Object.assign(dataSource[idex], values);
     this.setData({
       dataSource: dataSource || []
     })
   },
   valid( ) {
     const target = [
       'ballGrindingFineness',
       'glazeFlowRate',
       'glazeRatio',
       'glazingAmount',
       'recipeId',
       'screenMesh'
     ]
    const v = this.data.dataSource.every(item => {
      return  target.every(it => {
        return Boolean(item[it]) 
      })
    })
     
     return v;
   },
   validNumber( ){
     return this.data.dataSource.every(it=> {
      return it.valid !== false
     });
   },
   onSave() {
 
     if (this.valid() && this.validNumber()) {
       request.glazeAdd({
         id: this.data.glazeId,
         collectionRecordId: this.data.id,
         glazeDatum: this.data.dataSource,
         sortNumber: this.data.sortNumber
       }, () => {
         wx.navigateBack({
           delta: 1
         })
       })
     } else {
       console.log(this.data.dataSource)
       wx.showToast({
         title: '存在未填项或数值不规范',
         icon: 'none'
       })
     }
   },
   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function () {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function () {

   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function () {
     //  this.setDisbleAlert();
   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function () {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function () {

   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function () {

   }
 })